package com.api.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.IdUtil;
import com.api.domain.UserDTO;
import com.api.entity.*;
import com.api.log.Log;
import com.api.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.api.entity.table.AppVersionTableDef.APP_VERSION;
import static com.api.entity.table.BannerTableDef.BANNER;
import static com.api.entity.table.MenusTableDef.MENUS;

/**
 * @Description: 描述
 * @Author: yangshfieng
 * @CreateDate: 2023/6/25
 */

@Slf4j
@RestController
public class IndexController extends BaseController {

    @Log("获取首页")
    @RequestMapping("/index")
    public Result index(){
        Map<String,Object> res = new HashMap<String, Object>();
//        List<HashMap> banners = Banner.create()
//                .select(BANNER.TITLE, BANNER.PATH.as("url"), BANNER.REMARK, BANNER.LINK)
//                .where(Banner::getStatus).eq(1)
//                .orderBy(BANNER.SORT_NUM.asc())
//                .listAs(HashMap.class);
//        res.put("banners", banners);

        //今日任务
//        List<Task> tasks = Task.create().setStatus(1).orderBy(Task::getTaskTime, true).limit(10).list();
        //获取新闻
//        List<News> news = News.create().setStatus(2).orderBy(News::getSendTime, false).limit(5).list();
//        res.put("news", news);
        //获取行政公告
        List<Notices> notices = Notices.create().setStatus(2).orderBy(Notices::getSendTime, false).limit(5).list();
        res.put("notices", notices);
        return Result.ok(res);
    }

    @Log("获取版本信息")
    @RequestMapping("/appVersion")
    public Result appVersion(String osType,String appVersion){
        log.info("osType: {}, appVersion: {}", osType,appVersion);
        HashMap<String,Object> vCode = AppVersion.create()
                .select(
                        APP_VERSION.VERSION_CODE.as("versionCode"),
                        APP_VERSION.IS_FORCED.as("isForced"),
                        APP_VERSION.VERSION_DESC.as("versionDesc"),
                        APP_VERSION.DOWNLOAD_URL.as("downloadUrl"),
                        APP_VERSION.IS_APP_STORE.as("isAppStore")
                )
                .where(APP_VERSION.STATUS.eq(1))
                .and(APP_VERSION.OS_TYPE.eq(osType))
                .orderBy(APP_VERSION.ID.desc())
                .oneAs(HashMap.class);
        if(null == vCode){
            return Result.err("当前版本已是最新版本");
        }
        return Result.ok(vCode);
    }

    @RequestMapping("/getUserInfo")
    public Result userInfo(){
        return Result.ok(getUserInfo());
    }

    @Log("修改用户信息")
    @RequestMapping("/setUserInfo")
    public Result setUserInfo(@RequestBody UserDTO user) {
        boolean flag = User.create()
                .setId(getUserId())
                .setLoginName(user.getLoginName())
                .setUserName(user.getUserName())
                .setNickName(user.getNickName())
                .setUserTele(user.getUserTele())
                .setAvatar(user.getAvatar())
                .setSex(user.getSex())
                .updateById();
        if(flag){
            return Result.ok();
        }
        return Result.err();
    }

    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Log("修改密码")
    @RequestMapping("/setPwd")
    public Result setPwd(String oldPwd,String newPwd){
        if(StringUtils.isEmpty(oldPwd)){
            return Result.err("原始密码不能为空");
        }
        if(StringUtils.isEmpty(newPwd)){
            return Result.err("新密码不能为空");
        }
        User user = User.create()
                .where(User::getId).eq(getUserId())
                .one();
        if(null == user){
            return Result.err("未获取到当前登录用户信息，请重新登录");
        }
        String pwd = SaSecureUtil.md5BySalt(oldPwd,user.getSalt());
        if(!pwd.equalsIgnoreCase(user.getPassword())){
            return Result.err("原始密码不对");
        }
        String salt = IdUtil.simpleUUID().substring(0,10);
        String newPwdStr = SaSecureUtil.md5BySalt(newPwd,salt);
        boolean flag = user.create()
                .setId(getUserId())
                .setSalt(salt)
                .setPassword(newPwdStr)
                .updateById();
        if(flag){
            return Result.ok();
        }
        return Result.err();
    }
}
